package swingTests;

import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TextAreaTextFieldTest extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JTextField jTextField = null;
	private JButton jButton = null;
	private JTextArea jTextArea = null;

	/**
	 * This is the default constructor
	 */
	public TextAreaTextFieldTest() {
		super();
		initialize();
		addKeyListeners();
	}

	private void addKeyListeners() {

		Toolkit.getDefaultToolkit().addAWTEventListener(new TargetedMouseHandler(), AWTEvent.KEY_EVENT_MASK);
	}
	public class TargetedMouseHandler implements AWTEventListener
	{

//	    private Component parent;
//	    private Component innerBound;
//	    private boolean hasExited = true;
//
//	    public TargetedMouseHandler(Component p, Component p2)
//	    {
//	        parent = p;
//	        innerBound = p2;
//	    }

	    @Override
	    public void eventDispatched(AWTEvent e)
	    {
	    	if(e instanceof KeyEvent){
	    		KeyEvent ke= (KeyEvent)e;
	    		System.out.println(ke.getKeyChar());
	    	}
	    
	    	
//	    	
//	        if (e instanceof MouseEvent)
//	        {
//	            if (SwingUtilities.isDescendingFrom(
//	                (Component) e.getSource(), parent))
//	            {
//	                MouseEvent m = (MouseEvent) e;
//	                if (m.getID() == MouseEvent.MOUSE_ENTERED)
//	                {
//	                    if (hasExited)
//	                    {
//	                        System.out.println("Entered");
//	                        hasExited = false;
//	                    }
//	                } else if (m.getID() == MouseEvent.MOUSE_EXITED)
//	                {
//	                    Point p = SwingUtilities.convertPoint(
//	                        (Component) e.getSource(),
//	                        m.getPoint(),
//	                        innerBound);
//	                    if (!innerBound.getBounds().contains(p))
//	                    {
//	                        System.out.println("Exited");
//	                        hasExited = true;
//	                    }
//	                }
//	            }
	        }
	    }
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(744, 244);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
		this.setVisible(true);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJTextField());
			jContentPane.add(getJButton(), null);
			jContentPane.add(getJTextArea(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(32, 105, 152, 24));
		}
		return jTextField;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(208, 105, 75, 24));
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println(jTextField.getText());
					System.out.println("actionPerformed()"); // TODO
																// Auto-generated
																// Event stub
																// actionPerformed()
					jTextField.setText(jTextArea.getText());
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
			jTextArea.setBounds(new Rectangle(304, 26, 391, 158));
		}
		return jTextArea;
	}

	public static void main(String[] args) {
		new TextAreaTextFieldTest();
	}
} // @jve:decl-index=0:visual-constraint="10,10"
